package org.crystalrange.softwares.web;

import java.util.ArrayList;
import java.util.List;

import org.crystalrange.softwares.model.Software;
import org.crystalrange.softwares.model.SoftwareVersion;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Messagebox;

public class SoftwareVersionSelectController extends GenericForwardComposer {
	
	private Software software;
	protected SoftwareVersion selectedSoftwareVersion;
	protected List<SoftwareVersion> softwareVersions;
	
	public Software getSoftware() {
		return software;
	}

	public void setSoftware(Software software) {
		this.software = software;
	}

	public SoftwareVersion getSelectedSoftwareVersion() {
		return selectedSoftwareVersion;
	}

	public void setSelectedSoftwareVersion(SoftwareVersion selectedSoftwareVersion) {
		this.selectedSoftwareVersion = selectedSoftwareVersion;
	}

	public List<SoftwareVersion> getSoftwareVersions() {
		return softwareVersions;
	}

	public void setSoftwareVersions(List<SoftwareVersion> softwareVersions) {
		this.softwareVersions = softwareVersions;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.software = (Software) this.execution.getAttribute("software");
		this.softwareVersions = new ArrayList<SoftwareVersion>();
		softwareVersions = this.software.getSoftwareVersions();
		component.setAttribute("controller", this);
	}
	
	public void onClick$buttonOk(Event event) throws Exception {
		if (this.selectedSoftwareVersion != null) {
			this.execution.setAttribute("selectedSoftwareVersion",
					this.selectedSoftwareVersion);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("Please select a Software Version.");
		}
	}
	
	public void onClick$buttonCancel(Event event) throws Exception {
		this.execution.setAttribute("selectedSoftwareVersion", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}
	
}
